package com.chat.core.utils;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

public class Result {
    public static final String SUCCESS = "操作成功。";
    public static final String FAILURE = "操作失败！";

    private boolean result;
    private String msg;
    private Object data;
    private Integer code;
    private Long count;

    private Result() {}

    public static Result ok() {
        return Result.ok(SUCCESS);
    }

    public static Result ok(String msg) {
        return Result.ok(msg, null);
    }

    public static Result ok(Object data) {
        return Result.ok(SUCCESS, data);
    }

    public static Result ok(String msg, Object data) {
        Result result = new Result();
        if (data instanceof Page){
            Page page = (Page)data;
            result.setData(page.getRecords());
            result.setCount(page.getTotal());
        }else {
            result.setData(data);
        }
        result.setCode(0);
        result.setResult(true);
        result.setMsg(msg);
        return result;
    }

    public static Result fail() {
        return Result.fail(FAILURE);
    }

    public static Result fail(String msg) {
        return Result.fail(msg, null);
    }

    public static Result fail(Object data) {
        return Result.fail(FAILURE, data);
    }

    public static Result fail(String msg, Object data) {
        Result result = new Result();
        result.setResult(false);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public boolean isResult() {
        return result;
    }
    public void setResult(boolean result) {
        this.result = result;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Long getCount() {
        return count;
    }

    public void setCount(Long count) {
        this.count = count;
    }
}
